package com.chuanyang.driverh.mainorder;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainorder.adapter.OrderDriverAdapter;
import com.chuanyang.driverh.responseresult.LookWaybillData;
import com.chuanyang.driverh.responseresult.LookWaybillResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class OrderDriverListActivity extends BaseTitleActivity {

    private ListView lv_driver_list;
    private OrderDriverAdapter orderDriverAdapter;
    private List<LookWaybillData.DataBean> wayBillEntityList = new ArrayList<>();
    private String orderId = "";
    private String orderNo = "";
    private LookWaybillData lookWaybillData;
    private int type;//1表示从OrderLocationActivity进入   2表示从OrderHisFragment进入
    private EditText et_search_waybill;
    private LinearLayout ll_totalweight_div;
    private TextView tv_totalweight;
    private double totalweight=0;
    @Override
    protected int getContentResId(){return R.layout.order_act_driver_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        lookWaybillData = (LookWaybillData) getIntent().getSerializableExtra("lookWaybillData");
        lv_driver_list = (ListView) findViewById(R.id.lv_driver_list);
        ll_totalweight_div = (LinearLayout) findViewById(R.id.ll_totalweight_div);
        tv_totalweight = (TextView) findViewById(R.id.tv_totalweight);
        et_search_waybill = (EditText) findViewById(R.id.et_search_waybill);



    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("运单列表");
        type = getIntent().getIntExtra("type",0);
        if(lookWaybillData!=null&&lookWaybillData.getData().size()>0) {
            orderId=lookWaybillData.getData().get(0).getOrderId()+"";
            orderNo=lookWaybillData.getData().get(0).getOrderNo();
            orderDriverAdapter = new OrderDriverAdapter(OrderDriverListActivity.this, lookWaybillData.getData());
            lv_driver_list.setAdapter(orderDriverAdapter);
        }

    }

    @Override
    protected void initListener() {
        et_search_waybill.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_SEARCH) {
                    /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    if (TextUtils.isEmpty(et_search_waybill.getText().toString())) {
                        return false;
                    }
                    getWaybillList(et_search_waybill.getText().toString());
                    return true;
                }
                return false;
            }
        });

        lv_driver_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(type==1)
                {
                    Intent intent = new Intent();
                    LookWaybillData.DataBean bean= (LookWaybillData.DataBean) orderDriverAdapter.getItem(i);
                    intent.putExtra("data", bean);
                    setResult(1, intent);
                    //2.结束这个窗口
                    finish();
                }
                else {
                    Intent intent = new Intent(OrderDriverListActivity.this, LookWaybillDetailActivity.class);
                    intent.putExtra("LookWaybill", (Serializable) orderDriverAdapter.getItem(i));
                    startActivity(intent);
                }

            }
        });

    }
    //获得运单的列表
    private void  getWaybillList(String s){
        DialogUtil.showLoadingDialog(activityContext,"运单...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("orderId", orderId);
        params.put("orderNo", orderNo);
        params.put("page", "1");
        params.put("length", "400");
        params.put("plateNumber",s);
        MyHttpUtil.selectByOrderId(activityContext,params, new MyCallback<LookWaybillResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LookWaybillResult result, int id) {

                if (result.getCode() == ConstUtil.OK) {
                    wayBillEntityList.clear();
                    wayBillEntityList.addAll(result.getData().getData());
                    orderDriverAdapter.setOrderDirverEntityList(wayBillEntityList);
                    getRightTextView().setText("清除筛选");
                    if(wayBillEntityList.size()>0)
                    {
                        ll_totalweight_div.setVisibility(View.VISIBLE);
                        tv_totalweight.setText(getTotalWeight()+"t");
                    }
                    else {
                        ll_totalweight_div.setVisibility(View.GONE);

                    }
                }else {
                    wayBillEntityList.clear();
                    orderDriverAdapter.setOrderDirverEntityList(wayBillEntityList);
                    getRightTextView().setText("清除筛选");
                    ll_totalweight_div.setVisibility(View.GONE);
                    PrintUtil.toast(activityContext,"没有搜索到符合条件的运单！");



                }
            }
        });
    }

    @Override
    protected void baseGoEnsure() {
        if(TextUtils.isEmpty(mBaseEnsure.getText().toString()))
        {
            return;
        }
        else {
            if(lookWaybillData!=null&&lookWaybillData.getData().size()>0) {
                orderDriverAdapter.setOrderDirverEntityList(lookWaybillData.getData());
                getRightTextView().setText("");
                et_search_waybill.setText("");
                ll_totalweight_div.setVisibility(View.GONE);

            }
        }
    }

    private double getTotalWeight() {
        for(LookWaybillData.DataBean bean:wayBillEntityList)
        {
            try {
                double  weight=Double.parseDouble(bean.getWeight()+"");
                totalweight = totalweight +weight;
            }
            catch (Exception e)
            {
                LogUtils.e("OrderDriverListActivity","-----"+bean.getWeight());
            }
        }
        return  totalweight;
    }

}
